package com.taotao.rest.service.impl;

import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.rest.pojo.ItemCat;
import com.taotao.rest.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    /**
     * 查询商品类目展示到浏览器页面
     * @return
     */
    @Override
    public TaotaoResult getItemCat() {

        // 基于内存递归，只查询一次数据库
        TbItemCatExample condition = new TbItemCatExample();
        condition.createCriteria()
                .andStatusNotEqualTo(2);
        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(condition);
        List<Object> itemCatList = loadItemCatByMemory(tbItemCatList,0L);

        // 基于数据库查询递归
        //List<Object> itemCatList = loadItemCatByDatabase(0L);
        return TaotaoResult.ok(itemCatList);
    }

    /**
     * 基于内存递归，只查询一次数据库
     * @param tbItemCatList
     * @param parentId
     * @return
     */
    private List<Object> loadItemCatByMemory(List<TbItemCat> tbItemCatList,Long parentId){
        List<Object> result = new ArrayList<>();

        int count = 0;
        for (TbItemCat tbItemCat : tbItemCatList){
            if (tbItemCat.getParentId().equals(parentId)){
                if (tbItemCat.getIsParent()) {
                    ItemCat itemCat = new ItemCat();
                    itemCat.setUrl("/products/" + tbItemCat.getId() + ".html");
                    if (parentId.equals(0)) {
                        itemCat.setName("<a href='/products/" + tbItemCat.getId() + ".html'>" + tbItemCat.getName() + "</a>");
                    } else {
                        itemCat.setName(tbItemCat.getName());
                    }
                    itemCat.setItems(loadItemCatByMemory(tbItemCatList,tbItemCat.getId()));
                    result.add(itemCat);
                    count++;
                    if (count >= 14) {
                        break;
                    }
                } else {
                    String itemCat = "/products/" + tbItemCat.getId() + ".html|" + tbItemCat.getName();
                    result.add(itemCat);
                }
            }
        }
        return result;
    }


    /**
     * 基于数据库查询递归
     * @param parentId
     * @return
     */
    private List<Object> loadItemCatByDatabase(Long parentId) {
        List<Object> result = new ArrayList<>();

        TbItemCatExample condition = new TbItemCatExample();
        condition.createCriteria()
                .andStatusNotEqualTo(2)
                .andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(condition);

        int count = 0;
        for (TbItemCat tbItemCat : tbItemCatList) {
            if (tbItemCat.getIsParent()) {
                ItemCat itemCat = new ItemCat();
                itemCat.setUrl("/products/" + tbItemCat.getId() + ".html");
                if (parentId.equals(0)) {
                    itemCat.setName("<a href='/products/" + tbItemCat.getId() + ".html'>" + tbItemCat.getName() + "</a>");
                } else {
                    itemCat.setName(tbItemCat.getName());
                }
                itemCat.setItems(loadItemCatByDatabase(tbItemCat.getId()));
                result.add(itemCat);
                count++;
                if (count >= 14) {
                    break;
                }
            } else {
                String itemCat = "/products/" + tbItemCat.getId() + ".html|" + tbItemCat.getName();
                result.add(itemCat);
            }
        }
        return result;
    }
}
